home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / Sherlock 2.0 / DevLibSrc / Main_DevLib / LIBlist.h < prev    next >
Text File  |  1995-05-27  |  3KB  |  172 lines

  1. /*
  2.     devlib: header file containing list macros.
  3.     
  4.     source:  LIBlist.h
  5.     started: December 13, 1993.
  6.     version:
  7.         May 27, 1995.
  8.             Added lst2_free_all_macro.
  9.         March 17, 1994.
  10.             Added lst_general_reverse_macro.
  11.             Wrote lst_reverse_macro in terms of lst_general_reverse_macro.
  12.         March 6, 1994.
  13.             Bug fix: rewrote lst_reverse_macro.
  14. */
  15.  
  16. #ifndef LIBlist_h_
  17. #define LIBlist_h_
  18.  
  19. #pragma once
  20.  
  21. #define TYPE_LIST(type)\
  22.     type *    next    /* Pointer to next object. */
  23.  
  24. #define TYPE_LIST2(type)\
  25.     type *    next2;    /* Pointer to next object. */    \
  26.     type *    back2    /* Pointer to previous object. */
  27.  
  28.     /* Add a node to the beginning of a list. */
  29.  
  30. #define lst_add_macro(elem, list)\
  31. {\
  32.     ASSERT(elem);\
  33.     elem -> next = list;\
  34.     list = elem;\
  35. }
  36.  
  37. #define lst2_add_macro(elem, list)\
  38. {\
  39.     ASSERT(elem);\
  40.     if (list) {\
  41.         list -> back2 = elem;\
  42.     }\
  43.     elem -> next2 = list;\
  44.     elem -> back2 = NULL;\
  45.     list = elem;\
  46. }
  47.  
  48.     /* Append a node to the end of a list. */
  49.  
  50. #define lst_append_macro(elem, list, type)\
  51. {\
  52.     ASSERT(elem);\
  53.     elem -> next = NULL;\
  54.     if (list == NULL) {\
  55.         list = elem;\
  56.     }\
  57.     else {\
  58.         type * np_ = list;\
  59.         while(np_ -> next) {\
  60.             np_ = np_ -> next;\
  61.         }\
  62.         np_ -> next = elem;\
  63.     }\
  64. }
  65.  
  66. #define lst2_append_macro(elem, list, type)\
  67. {\
  68.     ASSERT(elem);\
  69.     elem -> next2 = NULL;\
  70.     if (list == NULL) {\
  71.         list = elem;\
  72.         elem -> back2 = NULL;\
  73.     }\
  74.     else {\
  75.         type * np_ = list;\
  76.         while(np_ -> next2) {\
  77.             np_ = np_ -> next2;\
  78.         }\
  79.         np_ -> next2 = elem;\
  80.         elem -> back2 = np_;\
  81.     }\
  82. }
  83.  
  84.     /*
  85.         Traverse a list.
  86.         The "safe" versions work even if node p is deleted.
  87.         This macro is an anachronism.
  88.     */
  89.  
  90. #define lst_every_macro(p,list)\
  91.      for(p=list; p; p=p->next)
  92.  
  93.     /* Free all nodes on a list. */
  94.  
  95. #define lst_free_all_macro(list, type)\
  96. {\
  97.     type * np_ = list;\
  98.     type * np2_;\
  99.     while(np_) {\
  100.         np2_ = np_ -> next;\
  101.         obj_free_macro(np_);\
  102.         np_ = np2_;\
  103.     }\
  104.     list = NULL;\
  105. }
  106.  
  107. /* Added 5/27/95. */
  108. #define lst2_free_all_macro(list, type)\
  109. {\
  110.     type * np_ = list;\
  111.     type * np2_;\
  112.     while(np_) {\
  113.         np2_ = np_ -> next2;\
  114.         obj_free_macro(np_);\
  115.         np_ = np2_;\
  116.     }\
  117.     list = NULL;\
  118. }
  119.  
  120.     /* Remove a node from a possibly empty list. */
  121.  
  122. #define lst_remove_macro(elem, list, type)\
  123. {\
  124.     type * np_ = list;\
  125.     type * np2_ = NULL;\
  126.     ASSERT(elem);\
  127.     if (list && elem && list == elem) {\
  128.         list = elem -> next;\
  129.     }\
  130.     else if (list && elem) {\
  131.         for(np2_ = np_ -> next; np2_; np_ = np2_, np2_ = np_ -> next) {\
  132.             if (np2_ == elem) {\
  133.                 np_ -> next = np2_ -> next;\
  134.                 break;\
  135.             }\
  136.         }\
  137.     }\
  138. }
  139.  
  140. #define lst2_remove_macro(elem, list)\
  141. {\
  142.     ASSERT(elem);\
  143.     if (elem -> back2 == NULL) {\
  144.         list = elem -> next2;\
  145.     }\
  146.     else {\
  147.         elem -> back2 -> next2 = elem -> next2;\
  148.     }\
  149.     if (elem -> next2) {\
  150.         elem -> next2 -> back2 = elem -> back2;\
  151.     }\
  152. }
  153.  
  154. #define lst_reverse_macro(the_list, the_type) \
  155.     lst_general_reverse_macro(the_list, next, the_type)
  156.  
  157. #define lst_general_reverse_macro(the_list, the_next_field, the_type)\
  158. {\
  159.     register the_type * new_p = NULL;\
  160.     register the_type * old_p = NULL;\
  161.     register the_type * next_p = NULL;\
  162.     \
  163.     for (old_p = (the_list); old_p; old_p = next_p) {\
  164.         next_p = old_p -> the_next_field;\
  165.         old_p -> the_next_field = new_p;\
  166.         new_p = old_p;\
  167.     }\
  168.     (the_list) = new_p;\
  169. }
  170.         
  171. #endif /* LIBlist_h_ */
  172.